package com.fanpan26.algo.sort.practice;

/**
 * @author fanpan26
 */
public class Day20191107Sort extends AbstractDaySort {

    public static void main(String[] args) {
        Day20191107Sort sort = new Day20191107Sort();
        sort.run();
    }

    /**
     * 冒泡排序
     *
     * @param arr
     */
    @Override
    protected int[] sortByBubble(int[] arr) {
        int temp = 0;
        boolean flag;
        for (int i = 0; i < arr.length; i++) {
            flag = false;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
        return arr;
    }

    /**
     * 插入排序
     *
     * @param arr
     */
    @Override
    protected int[] sortByInsertion(int[] arr) {
        for (int i=1;i<arr.length;i++){
            int value = arr[i];
            int j = i-1;
            for (;j>=0;j--){
                if (arr[j]>value){
                    arr[j+1]=arr[j];
                }else{
                    break;
                }
            }
            arr[j+1]=value;
        }
        return arr;
    }

    /**
     * 选择排序
     *
     * @param arr
     */
    @Override
    protected int[] sortBySelection(int[] arr) {
        int minIndex;

        for (int i = 0; i < arr.length-1; i++) {
            minIndex = i;
            for (int j = i+1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }

            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }

        return arr;
    }

    /**
     * 桶排序
     *
     * @param arr
     */
    @Override
    protected int[] sortByBucket(int[] arr) {
        return new int[0];
    }

    /**
     * 基数排序
     *
     * @param arr
     */
    @Override
    protected int[] sortByBase(int[] arr) {
        return new int[0];
    }

    /**
     * 计数排序
     *
     * @param arr
     */
    @Override
    protected int[] sortByCounting(int[] arr) {
        int max = 0;
        //先求出MAX
        for (int i=1;i<arr.length;i++){
            if (arr[i]>arr[max]){
                max = i;
            }
        }
        int maxValue = arr[max];
        //分成max个桶
        int[] c = new int[maxValue+1];
        //初始化数组
        for (int i=0;i<c.length;i++){
            c[i] = 0;
        }
        //开始计数
        for (int i=0;i<arr.length;i++){
            c[arr[i]]++;
        }
        //开始算名次
        for (int i=0;i<c.length-1;i++){
            c[i+1]=c[i]+c[i+1];
        }
        int[] r = new int[arr.length];
        for (int i=0;i<arr.length;i++){
            int index = c[arr[i]] -1;
            r[index]=arr[i];
            c[arr[i]]--;
        }
        return r;
    }

    /**
     * 快速排序
     *
     * @param arr
     */
    @Override
    protected int[] quickSort(int[] arr) {

        quickSortInternal(arr,0,arr.length-1);
        return arr;
    }

    private void quickSortInternal(int[] arr,int low,int high) {

        if (low<high) {
            int index = getPartitionIndex(arr, low, high);

            quickSortInternal(arr, low, index);
            quickSortInternal(arr, index + 1, high);
        }
    }

    private int getPartitionIndex(int[] arr,int low,int high){

        int value =arr[low];
        while (low<high){
            while (low<high && value<=arr[high]){
                high --;
            }
            arr[low] = arr[high];

            while (low < high && arr[low]<=value){
                low ++;
            }
            arr[high] = arr[low];
        }
        arr[low] = value;
        return low;
    }
}
